class Generator:
    def __init__(self, coeff, mod, inits):
        if len(coeff) != len(inits):
            raise Exception("coeff and inits arrays' length isn't equal")
        self.mod = mod
        self.coeff = coeff
        self.inits = inits
        self.prev = inits
        self.rec = len(coeff)

    def reinit(self):
        self.prev = self.inits

    def generate(self):
        ans = sum([self.coeff[x]*self.prev[x] for x in range(self.rec)]) % self.mod
        self.prev = self.prev[1:]+[ans]
        return ans

    def stress_analysis(self):
        self.reinit()
        d = {}
        self.generate()
        pos = 0
        while not (tuple(self.prev) in d):
            d[tuple(self.prev)] = pos
            pos += 1
            self.generate()
        P = d[tuple(self.prev)]
        L = pos - P
        return (P,L)

    def analysis(self, need_stress = False):
        L,PP = 0,1
        self.reinit()
        prev = self.prev
        while True:
            self.generate()
            L += 1
            if self.prev == prev:
                break
            if L == PP:
                L = 0
                PP = 2*PP
                prev = self.prev

        self.reinit()
        cp = Generator(self.coeff, self.mod, self.inits)
        for i in range(L):
          cp.generate()
        P = 0
        while self.prev != cp.prev:
            P += 1
            self.generate()
            cp.generate()

        if need_stress:
            stress = self.stress_analysis()
            if a != (P,L):
                raise Exception("stress test fault: "+str(stress)+" excepted but "+str((P,L))+" calced")
        return (P,L)

    def statistic(self, N = 20):
        stat = [0]*N
        self.reinit()
        for glob in range(N**2):
            stat[self.generate() / self.
